// service/user_service/user_scope.go
package user_service

import (
	"errors"
	"gorm.io/gorm"
	"muziai-server/models"
)

func (UserService) UserScope(tx *gorm.DB, userID uint, scope int) (err error) {
	// 要扣除积分
	var user models.UserModel
	err = tx.Take(&user, userID).Error
	if err != nil {
		return errors.New("用户不存在")
	}

	if scope == 0 {
		return nil
	}
	newScope := user.Scope + scope
	if newScope < 0 {
		return errors.New("用户积分不足")
	}

	// 扣积分
	tx.Model(&user).Update("scope", newScope)
	return nil
}
